import Vue from 'vue'
import VueRouter from 'vue-router'
import indexCom from '@/views/index/index.vue'
import enterCom from '@/views/enter/enter.vue'
import searchCom from '@/views/search/search.vue'
import listCom from '@/views/list/list.vue'
import detailCom from '@/views/detail/detail.vue'
import payCom from '@/views/pay/pay.vue'
import manageCom from '@/views/manage/manage.vue'
import homeCom from '@/views/index/home.vue'
import classCom from '@/views/index/class.vue'
import goodsCom from '@/views/index/goods.vue'
import userCom from '@/views/index/user.vue'
import store from '@/store'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/home' },
    {
      path: '/',
      component: indexCom,
      children: [
        { path: 'home', component: homeCom },
        { path: 'class', component: classCom },
        { path: 'goods', component: goodsCom },
        { path: 'user', component: userCom }
      ]
    },
    { path: '/enter', component: enterCom },
    { path: '/search', component: searchCom },
    { path: '/list', component: listCom },
    { path: '/detail', component: detailCom },
    { path: '/pay', component: payCom },
    { path: '/manage', component: manageCom }
  ]
})
const MYPath = ['/pay', '/manage', '/goods']
router.beforeEach((to, from, next) => {
  if (!MYPath.includes(to.path)) {
    next()
    return false
  }
  const token = store.state.user.info.token
  if (token) {
    next()
  } else {
    next('/enter')
  }
})
export default router
